package com.bilubi.reggie.reggie.controller;

import com.bilubi.reggie.reggie.bean.Employee;
import com.bilubi.reggie.reggie.common.R;
import com.bilubi.reggie.reggie.enums.CodeEnum;
import com.bilubi.reggie.reggie.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/employee")
@Slf4j
//@SessionAttributes("employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public R<Employee> login(@RequestBody Employee employee, Model model, HttpSession session) {
        Employee e = employeeService.getEmployeeByUsername(employee);
        if (e == null) {
            return R.error(CodeEnum.LOGIN_ERROR);
        }
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        if (!password.equals(e.getPassword())) {
            return R.error(CodeEnum.LOGIN_ERROR);
        }
        if (e.getStatus().equals(0)) {
            return R.error(CodeEnum.LOGIN_ERROR);
        }
//        model.addAttribute("employee", e.getId());
        session.setAttribute("employee", e);
        e.setPassword(null);
        return R.success(e);
    }

    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    public R<String> logout(HttpSession session) {
        session.removeAttribute("employee");
        return R.success();
    }

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public R<PageInfo<Employee>> getEmployeeList(@RequestParam(value = "page") int page,
                                                 @RequestParam(value = "pageSize") int pageSize,
                                                 @RequestParam(value = "name", required = false, defaultValue = "") String name){
        PageHelper.startPage(page, pageSize);
        List<Employee> employees = employeeService.getAllEmployees(name);
        PageInfo<Employee> pageInfo = new PageInfo<>(employees);
        return R.success(pageInfo);
    }

    @RequestMapping(method = RequestMethod.POST)
    public R<String> addEmployee(@RequestBody Employee employee, HttpSession session){
//        Employee e = (Employee) session.getAttribute("employee");
//        Long createUserId = e.getId();
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(new Date());
//        employee.setUpdateTime(new Date());
//        employee.setCreateUser(createUserId);
//        employee.setUpdateUser(createUserId);
        employeeService.addEmployee(employee);
        return R.success();
    }

    @RequestMapping(method = RequestMethod.PUT)
    public R<String> updateEmployee(@RequestBody Employee employee, HttpSession session){
        log.info("update employee:{}", employee);
        Employee e = (Employee) session.getAttribute("employee");
//        Long createUserId = e.getId();
//        Date updateTime = new Date();
//        employee.setUpdateTime(updateTime);
//        employee.setUpdateUser(createUserId);
        employeeService.updateEmployee(employee);
        return R.success();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public R<Employee> getEmployee(@PathVariable(value = "id") Long id){
        Employee employee = employeeService.getEmployeeById(id);
        employee.setPassword(null);
        return R.success(employee);
    }

}
